/*******************************************************************************
 * Package: com.hngtrust.iir.company.service.impl
 * Type:    CompReportServiceImpl
 * Date:    2021-5-28 15:23
 *
 * Copyright (c) 2021 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.hngtrust.iir.company.service.impl;

import com.hngtrust.framework.page.PageParam;
import com.hngtrust.framework.page.PageResult;
import com.hngtrust.iir.company.controller.vo.InfoHolderRespVO;
import com.hngtrust.iir.company.controller.vo.InfoPledgeRespVO;
import com.hngtrust.iir.company.controller.vo.InfoSubCompRespVO;
import com.hngtrust.iir.company.controller.vo.ListBankCreditRespVO;
import com.hngtrust.iir.company.controller.vo.ListHolderReqVO;
import com.hngtrust.iir.company.controller.vo.ListHolderRespVO;
import com.hngtrust.iir.company.controller.vo.ViewCompanyReqVO;
import com.hngtrust.iir.company.controller.vo.ViewCompanyRespVO;
import com.hngtrust.iir.company.dto.ListSubsidiaryDTO;
import com.hngtrust.iir.company.entity.CompBankCredit;
import com.hngtrust.iir.company.entity.CompFreezeDetail;
import com.hngtrust.iir.company.entity.CompHolder;
import com.hngtrust.iir.company.entity.CompPledgeDetail;
import com.hngtrust.iir.company.entity.CompStockPledge;
import com.hngtrust.iir.company.service.CompBankCreditService;
import com.hngtrust.iir.company.service.CompFreezeDetailService;
import com.hngtrust.iir.company.service.CompHolderService;
import com.hngtrust.iir.company.service.CompPledgeDetailService;
import com.hngtrust.iir.company.service.CompReportService;
import com.hngtrust.iir.company.service.CompService;
import com.hngtrust.iir.company.service.CompStockPledgeService;
import com.hngtrust.iir.constant.Constant;
import com.hngtrust.iir.dto.ReqDTO;
import com.hngtrust.iir.util.ListUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 研报生成的相关接口
 * @author Chentianyong
 * @date 2021-5-28 15:23
 */
@Service
public class CompReportServiceImpl implements CompReportService {
    
    @Autowired
    private CompService compService;
    
    @Autowired
    private CompHolderService compHolderService;
    
    @Autowired
    private CompStockPledgeService compStockPledgeService;
    
    @Autowired
    private CompPledgeDetailService compPledgeDetailService;
    
    @Autowired
    private CompFreezeDetailService compFreezeDetailService;
    
    @Autowired
    private CompBankCreditService compBankCreditService;
    
    @Override
    public InfoHolderRespVO infoHolder(ListHolderReqVO req) {
        //控股股东
        CompHolder compHolder = new CompHolder();
        compHolder.setCompId(req.getCompId());
        compHolder.setControlSharehol(1);
        List<CompHolder> list = compHolderService.queryList(compHolder);

        InfoHolderRespVO resp = new InfoHolderRespVO();
        
        for(CompHolder obj : list) {
            //控股股东
            if (null != obj.getControlSharehol() && obj.getControlSharehol().equals(1) && null != obj.getHolderId()) {
                ViewCompanyReqVO compReq = new ViewCompanyReqVO();
                compReq.setId(Long.valueOf(obj.getHolderId()));
                ViewCompanyRespVO compInfo = compService.viewCompany(compReq);
                if (null != compInfo) {
                    resp.setControlInfo(compInfo);
                }
                //股东列表
                ReqDTO<ListHolderReqVO> holderReq = new ReqDTO<ListHolderReqVO>();
                ListHolderReqVO content = new ListHolderReqVO();
                content.setCompId(Long.valueOf(obj.getHolderId()));
                holderReq.setContent(content);
                PageParam page = new PageParam();
                page.setPageNum(1);
                page.setPageSize(99);
                page.setOrderBy("stock_rate desc");
                holderReq.setPageParam(page);
                PageResult<ListHolderRespVO> holderList = compService.listHolder(holderReq);
                resp.setListHolder(holderList.getRows());
            }
        }
        //实际控制人
        compHolder = new CompHolder();
        compHolder.setCompId(req.getCompId());
        compHolder.setControllerIsAnn(1);
        list = compHolderService.queryList(compHolder);
        for(CompHolder obj : list) {
            //实际控制人
            if(null != obj.getControllerIsAnn() && obj.getControllerIsAnn().equals(1) && null != obj.getHolderId()){
                ViewCompanyReqVO compReq = new ViewCompanyReqVO();
                compReq.setId(Long.valueOf(obj.getHolderId()));
                ViewCompanyRespVO compInfo = compService.viewCompany(compReq);
                if(null != compInfo){
                    resp.setRealControlInfo(compInfo);
                }
            }
        }
        
        return resp;
    }
    
    @Override
    public InfoPledgeRespVO infoPledge(ListHolderReqVO req) {
        InfoPledgeRespVO resp = new InfoPledgeRespVO();
        
        //公司股权质押
        CompStockPledge pledge = new CompStockPledge();
        pledge.setCompId(req.getCompId());
        pledge.setStatus(Constant.DEFAULT_STATUS_NORMAL);
        List<CompStockPledge> pledges = compStockPledgeService.queryPledge(pledge);
        if(!pledges.isEmpty()){
            if(pledges.size() > 99){
                pledges = pledges.subList(0,99);
            }
            resp.setListPledge(pledges);
        }
    
        //公司股权质押明细
        CompPledgeDetail pledgeDetail = new CompPledgeDetail();
        pledgeDetail.setCompId(req.getCompId());
        pledgeDetail.setStatus(Constant.DEFAULT_STATUS_NORMAL);
        List<CompPledgeDetail> pledgeDetails = compPledgeDetailService.queryList(pledgeDetail);
        if(!pledgeDetails.isEmpty()){
            if(pledgeDetails.size() > 99){
                pledgeDetails = pledgeDetails.subList(0,99);
            }
            resp.setListPledgeDetail(pledgeDetails);
        }
    
        //公司股权冻结明细
        CompFreezeDetail freezeDetail = new CompFreezeDetail();
        freezeDetail.setCompId(req.getCompId());
        freezeDetail.setStatus(Constant.DEFAULT_STATUS_NORMAL);
        List<CompFreezeDetail> compFreezeDetails = compFreezeDetailService.queryList(freezeDetail);
        if(!compFreezeDetails.isEmpty()){
            if(pledges.size() > 99){
                pledges = pledges.subList(0,99);
            }
            resp.setListFreezeDetail(compFreezeDetails);
        }
        
        return resp;
    }
    
    @Override
    public InfoSubCompRespVO infoSubComp(ListHolderReqVO req) {
        ReqDTO<ListHolderReqVO> holderReq = new ReqDTO<ListHolderReqVO>();
        ListHolderReqVO content = new ListHolderReqVO();
        content.setCompId(req.getCompId());
        holderReq.setContent(content);
        PageParam page = new PageParam();
        page.setPageNum(1);
        page.setPageSize(99);
        holderReq.setPageParam(page);
        PageResult<ListSubsidiaryDTO> list = compService.listSubsidiary(holderReq);
    
        InfoSubCompRespVO resp = new InfoSubCompRespVO();
        if(null == list || null == list.getRows() || list.getRows().isEmpty()) {
            return null;
        }
        resp.setListSubComp(list.getRows());
        
        //读取对应股东信息
        List<ListHolderRespVO> listHolder = new ArrayList<>();
        for(ListSubsidiaryDTO sub : list.getRows()){
            if(null != sub.getSubCompId()){
                //股东列表
                content = new ListHolderReqVO();
                content.setCompId(sub.getSubCompId());
                holderReq.setContent(content);
                PageResult<ListHolderRespVO> holders = compService.listHolder(holderReq);
                if(null == list || null == list.getRows() || list.getRows().isEmpty()) {
                    break;
                }
                //处理公司名称
                for(ListHolderRespVO obj : holders.getRows()){
                    obj.setCompName(sub.getName());
                }
                listHolder.addAll(holders.getRows());
            }
        }
        resp.setListHolder(listHolder);
        
        return resp;
    }
    
    @Override
    public List<ListBankCreditRespVO> listBankCredit(ListHolderReqVO req) {
        CompBankCredit compBankCredit = new CompBankCredit();
        compBankCredit.setCompId(req.getCompId());
        compBankCredit.setStatus(Constant.DEFAULT_STATUS_NORMAL);
        List<CompBankCredit> list = compBankCreditService.queryList(compBankCredit);
        if(null != list && list.size() > 99){
            list = list.subList(0,99);
        }
        
        List<ListBankCreditRespVO> resp = new ArrayList<>();
        ListUtil<ListBankCreditRespVO> listUtil = BeanUtils.instantiateClass(ListUtil.class);
        listUtil.copyList(list,resp,ListBankCreditRespVO.class);
    
        return resp;
    }
}
